package com.txl.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.txl.dao.ContactDao;
import com.txl.domain.ContactBean;
import com.txl.exception.DaoException;
import com.txl.pager.PageBean;
import com.txl.pager.QueryInfo;
import com.txl.pager.QueryResult;

public class ContactService {

	private ContactDao dao = new ContactDao();
	
	//查找联系人
	public ContactBean findContact(String contactid){
		return dao.find(contactid);
	}
	
	//根据指定的分组id来查找联系人
	public List<ContactBean> findByGroupID(String groupid){
		return dao.findByGroupID(groupid);
	}
	
	//添加联系人
	public void addContact(ContactBean contact){
		contact.setContactid(UUID.randomUUID().toString());
		dao.add(contact);
	}
	
	//修改联系人
	public void updateContact(ContactBean contact){
		dao.update(contact);
	}
	
	//删除联系人
	public void deleteContact(String contactid){
		dao.delete(contactid);
	}
	
	//获取所有联系人
	public List<ContactBean> getAll(){
		return dao.getAll();
	}
	
	//全局分页显示
	public PageBean pageQuery(QueryInfo queryinfo){
		
		//调用dao获取到页面数据
		QueryResult qr = dao.pageQuery(queryinfo.getStartindex(), queryinfo.getPagesize());
		
		//根据dao查询结果，生成页面显示需要pagebean
		PageBean bean = new PageBean();
		bean.setCurrentpage(queryinfo.getCurrentpage());
		bean.setList(qr.getList());
		bean.setPagesize(queryinfo.getPagesize());
		bean.setTotalrecord(qr.getTotalrecord());
		
		return bean;
	}
	
	//带有条件的分页显示
	public PageBean pageQueryByItem(QueryInfo queryinfo){
		
		//调用dao获取到页面数据
		QueryResult qr = dao.pageQuery(queryinfo.getStartindex(), queryinfo.getPagesize(),queryinfo.getGroupid());
		
		//根据dao查询结果，生成页面显示需要pagebean
		PageBean bean = new PageBean();
		bean.setCurrentpage(queryinfo.getCurrentpage());
		bean.setList(qr.getList());
		bean.setPagesize(queryinfo.getPagesize());
		bean.setTotalrecord(qr.getTotalrecord());
		
		return bean;
	}
	
	//模糊查找
	public List<ContactBean> search(String key){
		return dao.search(key);
	}
}

